package com.bw.goods.service.impl;

import com.bw.goods.domain.*;
import com.bw.goods.mapper.*;
import com.bw.goods.service.DetailsInformationService;
import com.bw.goods.service.IPmsSkuInfoService;
import com.bw.goods.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author：尚志豪
 * @Package：com.bw.goods.service.impl
 * @Project：ruoyiszh
 * @name：DetailsInformationServiceImpl
 * @Date：2024/12/23 20:34
 */
@Service
public class DetailsInformationServiceImpl implements DetailsInformationService {
    @Autowired
    private DetailsInformationMapper detailsInformationMapper;

    @Autowired
    private IPmsSkuInfoService PmsSkuInfoService;

    @Autowired
    private PmsSkuImagesMapper skuImagesMapper;

    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;

    @Autowired
    private PmsSkuSaleAttrValueMapper skuSaleAttrValueMapper;

    @Autowired
    private PmsSpuInfoDescMapper spuInfoDescMapper;

    @Autowired
    private PmsAttrGroupMapper attrGroupMapper;
    /**
     * 1.查sku详情   0.5s
     * 2.查sku图片信息 1s
     * 3.查spu相关信息销售属性 1s
     * 4.查spu商品介绍 1s
     * 5.查规格和包装 05s
     */
    @Override
    public ItemInfoVo selectSkuItemInfoBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        ItemInfoVo itemInfoVo = new ItemInfoVo();

//        1.查sku详情   0.5s
        CompletableFuture<PmsSkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfo = PmsSkuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        },itemThreadPoolExcuter);

//        2.查sku图片信息 1s
        CompletableFuture<Void> skuImageCompletableFuture = CompletableFuture.runAsync(() -> {
            PmsSkuImages skuImages = new PmsSkuImages();
            skuImages.setSkuId(skuId);
            List<PmsSkuImages> pmsSkuImages = skuImagesMapper.selectPmsSkuImagesList(skuImages);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        },itemThreadPoolExcuter);

//        3.查spu相关信息销售属性 1s
        CompletableFuture<Void> skuSaleAttrValueCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<SaleAttrVo> saleAttrVos = skuSaleAttrValueMapper.selectSkuSaleAttrs(skuInfo.getSpuId());
            itemInfoVo.setSaleAttrs(saleAttrVos);
        },itemThreadPoolExcuter);

//        4.查spu商品介绍 1s
        CompletableFuture<Void> spuInfoDescCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = spuInfoDescMapper.selectPmsSpuInfoDescBySpuId(skuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        },itemThreadPoolExcuter);

//        5.查规格和包装 05s
        CompletableFuture<Void> attrGroupCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<AttrGroupVo> attrGroupVos = attrGroupMapper.selectAttrGroupVos(skuInfo.getSpuId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        },itemThreadPoolExcuter);

        CompletableFuture.allOf(skuImageCompletableFuture,skuSaleAttrValueCompletableFuture,attrGroupCompletableFuture,spuInfoDescCompletableFuture).get();

        return itemInfoVo;
    }
    /**
     * 添加购物车
     */
    @Override
    @Transactional
    public void addSkuItemInfoShoppingCart(PmsSkuInfo pmsSkuInfo) {
        // 获取品牌店铺id
        Long brandId = pmsSkuInfo.getBrandId();
        // 获取skuId
        Long skuId = pmsSkuInfo.getSkuId();
        // 购物车内是否存在对应的品牌id  根据brandId查询品牌
        Cart cart = detailsInformationMapper.searchForBrandBasedOnBrandid(brandId);
        System.out.println("品牌店铺内容:"+cart);
        // 查看品牌店铺内容是否存在
        if (cart == null)
        {
            // 添加购物车内品牌店铺
            detailsInformationMapper.addBrandToCart(brandId);
        }
        // 通过skuId是否有同样的商品
        ShoppingCart shoppingCart = detailsInformationMapper.isThereASimilarProductAvailableThroughSkuid(skuId);
        // 查看商品内容是否存在
        if (shoppingCart != null)
        {
            // 修改商品的数量
            detailsInformationMapper.modifyTheQuantityOfTheProduct(skuId);
        }

        // 把商品详情放入购物车内
        if (shoppingCart == null)
        {

            detailsInformationMapper.putTheDetailsInTheShoppingCart(pmsSkuInfo);
        }

    }
    /**
     * 购物车列表展示功能
     */
    @Override
    public ShoppingCartListDisplayFunctionVo searchAllShoppingCarts() {
        ShoppingCartListDisplayFunctionVo shoppingCartListDisplayFunctionVo = new ShoppingCartListDisplayFunctionVo();
        // 查询品牌店铺
        List<Cart> carts = detailsInformationMapper.cartsList();
        shoppingCartListDisplayFunctionVo.setCart(carts);
        // 查询商品
        List<ShoppingCart> shoppingCart = detailsInformationMapper.shoppingCartList();
        shoppingCartListDisplayFunctionVo.setShoppingCartList(shoppingCart);

        return shoppingCartListDisplayFunctionVo;
    }
    /**
     * 删除购物车
     */
    @Override
    @Transactional
    public void deleteShoppingCart(Long skuId) {
        // 查询商品
        ShoppingCart shoppingCart = detailsInformationMapper.selectShoppingCartList(skuId);
        // 品牌ID
        Long brandId = shoppingCart.getBrandId();
        // 通过skuId删除购物车
        detailsInformationMapper.deleteThroughSkuid(skuId);
        // 查询所有对应品牌的商品
        List<ShoppingCart> shoppingCarts = detailsInformationMapper.shoppingCartBrandIdList(brandId);
        // 对应的品牌店铺没有对应商品就删除品牌
        if (shoppingCarts.size()==0)
        {
            // 删除品牌店铺
            detailsInformationMapper.deleteBrandId(brandId);
        }
    }
    /**
     * 购物车列表展示功能2
     */
    @Override
    public List<ShoppingCartVo> searchAllShoppingCarts2() {
        // 查询所有
        List<NewShoppingCart> cartList = detailsInformationMapper.newShoppingCartList();
        // 按照品牌id分组
        Map<Long, List<NewShoppingCart>> groupedByBrandId = cartList.stream()
                .collect(Collectors.groupingBy(NewShoppingCart::getShopsId));

        return groupedByBrandId.entrySet().stream()
                .map(entry -> new ShoppingCartVo(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
    }


}
